<?php

namespace App\Services\Common;

use Illuminate\Http\Request;

use App\Models\Common\TableExtFeild as CommonTableExtFeild;

class TableExtFeild
{
    /**
     * 获取表的扩展字段标题
     */
    public static function getFeildTitle($tablename,$cpid)
    {
        $list = CommonTableExtFeild::where(['tablename' => $tablename,'cpid'=>$cpid])->select(['field_1', 'field_2', 'field_3', 'field_4', 'field_5','field_6', 'field_7', 'field_8', 'field_9', 'field_10'])->first();
        $list=$list?$list->toArray():[];
        foreach ($list as $key => $val) {
            if ($val == '') {
                unset($list[$key]);
            }
        }
        return $list;
    }

    /**
     * 编辑表的扩展字段标题
     */
    public static function setFeildTitle($tablename, $params)
    {
        $info = CommonTableExtFeild::where(['tablename' => $tablename])->first();
        if ($info) {
            CommonTableExtFeild::where(['tablename' => $tablename])->update($params);
        } else {
            $params['tablename'] = $tablename;
            CommonTableExtFeild::create($params);
        }
    }

    /**
     * 数据拼接
     */
    public static function setFeildToData($fielddata,$dataarr){
        for($i=1;$i<=10;$i++){
            if(isset($fielddata['field_'.$i]) && $fielddata['field_'.$i]!=''){
                $dataarr['field_'.$i]= $fielddata['field_'.$i];
            }
        }
        return $dataarr;
    }

    /**
     * 获取表头字段数据
     */
    public static function getTableFeildData(Request $request){
        $req_data = $request->only([
            'field_1', 'field_2', 'field_3', 'field_4', 'field_5', 'field_6', 'field_7', 'field_8', 'field_9', 'field_10'
        ]);
        $dataarr=[];
        for($i=1;$i<=10;$i++){
            if(isset($req_data['field_'.$i]) && $req_data['field_'.$i]!=''){
                $dataarr['field_'.$i]= $req_data['field_'.$i];
            }
        }
        return $dataarr;
    }
}
